package com.atguigu.gulimall.order.service.impl;

import com.alipay.api.AlipayApiException;
import com.atguigu.gulimall.order.service.PayStrategy;
import com.atguigu.gulimall.order.vo.PayAsyncVO;
import com.atguigu.gulimall.order.vo.PayVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

/**
 *
 * @ClassName AliPayStrategy
 * @Description 支付宝策略角色实现
 * @Author xufb
 * @Date 2024/5/11 18:06:40
 * @Version 1.0
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class AliPayStrategy implements PayStrategy {

    private final AliPayServiceImpl aliPayService;

    /**
     * 创建支付
     * @param order 订单详情
     */
    @Override
    @Transactional
    public String pay(PayVO order) throws AlipayApiException {
        // 创建支付返回html渲染
        return aliPayService.pay(order);
    }

    @Override
    public Boolean notify(HttpServletRequest request, PayAsyncVO asyncVo) throws AlipayApiException {
        // 验签
        Boolean signVerified = aliPayService.verify(request);
        if (Boolean.TRUE.equals(signVerified)) {
            // 修改订单状态
            log.info("签名验证成功...修改订单状态");
            aliPayService.handlePayResult(asyncVo);
        } else {
            log.info("签名验证失败...");
        }
        return signVerified;
    }
}
